<?php
require_once('../../../../wp-load.php');

global $wpdb, $current_user;
get_currentuserinfo();
if (!is_user_logged_in()){
	echo json_encode("{rs: 2, 'msg:'nologin'}");
	exit;	
}

if(isset($_REQUEST["action"]) && strcmp($_REQUEST["action"],"insert_category")==0 ){
	$value = $_REQUEST["value"];	

	$my_cat = array('category_description' => $value , 'category_nicename' => '', 'category_parent' => '');
	// Create the category
	// $my_cat_id =  wp_insert_term($value, "category", $my_cat);
	$my_cat_id =  ys_insert_category($value, "category", $my_cat);
	if(!is_wp_error($my_cat_id)){
		$insertRs = insertNewCategoryRelationship($my_cat_id["term_id"], $current_user->ID);
		if($insertRs > 0){
			echo json_encode(array('rs' => 0,
				'category_id' => $my_cat_id["term_id"]
			 ));
			exit;	
		}else{
			echo json_encode(array('rs' => 3,'msg'=>'insert relationship fail'));
			exit;		
		}
		
	}
	else{
		echo json_encode(array('rs' => 1,'msg' =>'unknown'));
		exit;	
	}
}
?>